layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 盘点项管理
     */
    var CheckStockItem = {
        tableId: "checkStockItemTable"
    };

    /**
     * 初始化表格的列
     */
    CheckStockItem.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'productId', sort: false, title: '商品ID'},
            {field: 'shelfId', sort: false, title: '货架位ID'},
            {field: 'warehouseId', sort: false, title: '仓库ID'},
            {field: 'shopId', sort: false, title: '店铺ID'},
            {field: 'creatorId', sort: false, title: '创建者（盘点者）'},
            {field: 'count', sort: false, title: '盘点的实际数量'},
            {field: 'countChange', sort: false, title: '盘点的数量差值'},
            {field: 'status', sort: false, title: '状态 (0:未审核 1：已审核）'},
            {field: 'checkMessage', sort: false, title: '盘点备注'},
            {field: 'checkTime', sort: false, title: '盘点时间'},
            {field: 'checkUserId', sort: false, title: '盘点人'},
            {field: 'auditTime', sort: false, title: '审核时间'},
            {field: 'auditUserId', sort: false, title: '审核人'},
            {field: 'auditMessage', sort: false, title: '审核备注'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    CheckStockItem.search = function () {
        var queryData = {};

        queryData['productId'] = $('#productId').val();
        queryData['shelfId'] = $('#shelfId').val();
        queryData['warehouseId'] = $('#warehouseId').val();
        queryData['shopId'] = $('#shopId').val();
        queryData['status'] = $('#status').val();

        table.reload(CheckStockItem.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    CheckStockItem.openAddDlg = function () {
        func.open({
            title: '添加盘点项',
            content: Feng.ctxPath + '/checkStockItem/add',
            tableId: CheckStockItem.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      CheckStockItem.openEditDlg = function (data) {
          func.open({
              title: '修改盘点项',
              content: Feng.ctxPath + '/checkStockItem/edit?id=' + data.id,
              tableId: CheckStockItem.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    CheckStockItem.exportExcel = function () {
        var checkRows = table.checkStatus(CheckStockItem.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    CheckStockItem.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/checkStockItem/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(CheckStockItem.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + CheckStockItem.tableId,
        url: Feng.ctxPath + '/checkStockItem/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: CheckStockItem.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        CheckStockItem.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    CheckStockItem.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        CheckStockItem.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + CheckStockItem.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            CheckStockItem.openEditDlg(data);
        } else if (layEvent === 'delete') {
            CheckStockItem.onDeleteItem(data);
        }
    });
});
